/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bitzer.google;

import com.google.gdata.client.DocumentQuery;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fstech
 */

    public class Documents {

    private static Documents _instance  =   null;
    private static DocumentList documentList  ;
    public int recordCount  = 0;

    public static Documents instance(){
        if(_instance == null){
            _instance = new Documents();
        }
        return _instance;
    }

    public Documents(){
        try {
            documentList = new DocumentList(Constants.APPLICATION_NAME, DocumentList.DEFAULT_HOST);
        } catch (DocumentListException ex) {
            Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void authenticate(String username, String password) throws AuthenticationException {

        if (username == null || password == null) {
            return;
        }
        try {
            documentList.login(username, password);
        } catch (DocumentListException ex) {
            Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   public DocumentListFeed getFeed(String lastModifiedOn, int startIndex, int numberOfRows) {
      
         DocumentListFeed feed = null;
         try {

            feed = documentList.getDocsListFeed("folders" ,startIndex, numberOfRows );
            recordCount = feed.getEntries().size();
            
            return feed;
        } catch (Exception e) {
            return null;
        }
    }

   public DocumentListFeed getDocumentListFeed(String lastModifiedOn, int startIndex, int numberOfRows , String resourceId) {

         DocumentListFeed feed = null;
         try {

            feed = documentList.getFolderDocsListFeed(resourceId);
            recordCount = feed.getEntries().size();

            return feed;
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String args[]) throws DocumentListException, AuthenticationException, IOException, MalformedURLException, ServiceException{


        String username = "jose@web1infotech.com";//parser.getValue("username", "user", "u");
        String password = "WebilPle*J";//parser.getValue("password", "pass", "p");
        String host =  DocumentList.DEFAULT_HOST;

        DocumentList documentList = new DocumentList(Constants.APPLICATION_NAME, host);
        documentList.login(username, password);
        DocumentListFeed feed = documentList.getDocsListFeed("folders" , 1, 500);

         if (feed != null) {
            for (DocumentListEntry entry : feed.getEntries()) {
               System.out.println(entry.getTitle().getPlainText());
            }
        } else {
            System.out.println("Nothing Found");
        }
    }

}
